package cn.yougou.controller;

import cn.yougou.common.DtoUtil;
import cn.yougou.common.RedisUtil;
import cn.yougou.dto.Dto;
import cn.yougou.pojo.YgCategory;
import cn.yougou.service.shopcart.YgCategoryService;
import cn.yougou.vo.YgCategoryVo;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * (YgCategory)表控制层
 *
 * @author 白杰崇
 * @since 2020-12-03 20:34:53
 */
@RestController
@RequestMapping("ygCategory")
public class YgCategoryController {
    /**
     * 服务对象
     */
    @Reference
    private YgCategoryService ygCategoryService;

    @Autowired
    private  RedisUtil redisUtil;

    /**
     * 分类页面：查询一二三级分类全部详情
     * @return
     */
    @GetMapping("/categoryAll")
    public Dto categoryAll() {
        Dto dto = null;
        try {
            //分类页面默认 首先查询一级类目，传参为0
            YgCategory ygCategory = new YgCategory(0);
            List<YgCategoryVo> allList =  ygCategoryService.categoryAll(ygCategory);
            dto = DtoUtil.returnSuccess("查询成功",allList);
        }catch (Exception e) {
            e.printStackTrace();
            dto = DtoUtil.returnFail("系统错误！","49999");
        }
        return dto;
    }

    /**
     * 商品列表页面根据一级标题id查询二级三级分类（商品列表侧边栏功能）
     * 参数：二级分类pid
     * @return
     */
    @GetMapping("/categoryById")
    public Dto categoryById(Integer fid){
        Dto dto = null;
        try {
            YgCategory ygCategory = new YgCategory(fid);
            List<YgCategoryVo> list = ygCategoryService.categoryById(ygCategory);
            dto = DtoUtil.returnSuccess("查询成功",list);
        }catch (Exception e) {
            e.printStackTrace();
            dto = DtoUtil.returnFail("系统错误！","49999");
        }
        return dto;
    }

    @GetMapping("redis")
    public void getRedis(){
        redisUtil.setStr("登庭2","111111111");
    }

}